package com.qtjf.qtmoneyservice.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qtjf.qtmoneyservice.MyApplication;
import com.qtjf.qtmoneyservice.R;
import com.qtjf.qtmoneyservice.constantSet.Constant;
import com.qtjf.qtmoneyservice.domain.BaseInfo;
import com.qtjf.qtmoneyservice.domain.Token;
import com.qtjf.qtmoneyservice.widget.AddressDialog;
import com.qtjf.qtmoneyservice.widget.EducationDialog;
import com.qtjf.qtmoneyservice.widget.HunYinDialog;
import com.qtjf.qtmoneyservice.widget.ShiChangDialog;
import com.qtjf.qtmoneyservice.widget.ZiNvDialog;

import java.util.HashMap;
import java.util.Map;

public class BaseInfoActivity extends BaseActivity {

    ImageView mBack;

    TextView xueLiText;
    TextView hunYinText;
    TextView ziNvText;
    TextView diZhiText;
    TextView shiChangText;
    EditText qqText;
    EditText emailText;
    TextView submitBt;
    EditText dizhiDetailText;
    String authenticationId;
    BaseInfo baseInfo;
    Gson gson;

    Handler handler=new Handler(){
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            if(msg.arg1==0)
            {
                xueLiText.setText(msg.obj.toString());
            }
            else if(msg.arg1==1)
            {
                hunYinText.setText(msg.obj.toString());
            }
            else if(msg.arg1==2)
            {
                ziNvText.setText(msg.obj.toString());
            }
            else if(msg.arg1==3)
            {
                diZhiText.setText(msg.obj.toString());
            }
            else if(msg.arg1==4)
            {
                shiChangText.setText(msg.obj.toString());
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_info);
        super.setTranslucent();
        Intent intent=getIntent();
        authenticationId=intent.getStringExtra("authenticationId");

        initView();
        initData();
        initEvent();
    }

    private void initView() {
        gson=new Gson();
        mBack = (ImageView) findViewById(R.id.back_bt);

        xueLiText= (TextView) findViewById(R.id.xueli_text);
        hunYinText= (TextView) findViewById(R.id.hunyin_text);
        ziNvText= (TextView) findViewById(R.id.zinv_text);
        diZhiText= (TextView) findViewById(R.id.dizhi_text);
        shiChangText= (TextView) findViewById(R.id.shichang_text);
        qqText= (EditText) findViewById(R.id.qq_text);
        emailText= (EditText) findViewById(R.id.email_text);
        submitBt= (TextView) findViewById(R.id.submit_bt);
        dizhiDetailText= (EditText) findViewById(R.id.dizhi_detail_text);
    }

    private void initData() {
        getInfo();
    }

    private void initEvent() {
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseInfoActivity.this.finish();
            }
        });


        xueLiText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EducationDialog dialog=new EducationDialog(BaseInfoActivity.this,R.style.myTransparent,handler);
                dialog.show();

            }
        });
        hunYinText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                HunYinDialog dialog=new HunYinDialog(BaseInfoActivity.this,R.style.myTransparent,handler);
                dialog.show();
            }
        });

        ziNvText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ZiNvDialog dialog=new ZiNvDialog(BaseInfoActivity.this,R.style.myTransparent,handler);
                dialog.show();
            }
        });

        diZhiText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                AddressDialog dialog=new AddressDialog(BaseInfoActivity.this,R.style.myTransparent,handler);
                dialog.show();
            }
        });

        shiChangText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ShiChangDialog dialog=new ShiChangDialog(BaseInfoActivity.this,R.style.myTransparent,handler);
                dialog.show();
            }
        });

        submitBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submitInfo();
            }
        });
    }


    private void submitInfo() {
        String url= Constant.HOMEADDRESS + "jdt/submitUserBase";
        final StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                // TODO Auto-generated method stub
                Toast.makeText(BaseInfoActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                if(response.length()>10) {
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                Toast.makeText(BaseInfoActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
            }

        }) {
            // 需要重写获取参数的函数,可以向服务器提交参数
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("userId","1");
                map.put("education",xueLiText.getText().toString().trim());
                map.put("marriage",hunYinText.getText().toString().trim());
                map.put("children",ziNvText.getText().toString());//ziNvText.getText().toString()需要后台改成String
                map.put("provinceId",diZhiText.getText().toString());
                map.put("cityId","");
                map.put("domain",dizhiDetailText.getText().toString());
                map.put("qq",qqText.getText().toString());
                map.put("liveTime",shiChangText.getText().toString());
                map.put("email",emailText.getText().toString());
                return map;
            }

        };


        MyApplication.getQueue().add(stringRequest);

    }

    //GET 开发完成 http://118.89.185.176:8082/jdt/getAuthenticationInfo?type={type}&id={id}


    private void getInfo() {
        String url= Constant.HOMEADDRESS + "jdt/getAuthenticationInfo?type="+"0"+"&id="+authenticationId;
        final StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                // TODO Auto-generated method stub
               // Toast.makeText(BaseInfoActivity.this, response, Toast.LENGTH_SHORT).show();
                Log.d("response",response);
                if(response.length()>10) {
                    baseInfo = gson.fromJson(response, new TypeToken<BaseInfo>() {
                    }.getType());
                    if(baseInfo.getData().getEducation()!=null) {
                        xueLiText.setText(baseInfo.getData().getEducation());
                    }else{
                        xueLiText.setText("请选择");
                    }
                    if(baseInfo.getData().getMarriage()!=null)
                    {
                        hunYinText.setText(baseInfo.getData().getMarriage());
                    }else{
                        hunYinText.setText("请选择");
                    }

                    if(baseInfo.getData().getChildren()!=null)
                    {
                        ziNvText.setText(baseInfo.getData().getChildren());
                    }else{
                        ziNvText.setText("请选择");
                    }

                    if(baseInfo.getData().getProvinceId()!=null)
                    {
                        diZhiText.setText((String)baseInfo.getData().getProvinceId());
                    }else{
                        diZhiText.setText("请选择");
                    }

                    if(baseInfo.getData().getLiveTime()!=null)
                    {
                        shiChangText.setText((String)baseInfo.getData().getLiveTime());
                    }
                    else{
                        shiChangText.setText("请选择");
                    }
                    if(baseInfo.getData().getQq()!=null)
                    {
                        qqText.setText(baseInfo.getData().getQq());
                    }
                    if(baseInfo.getData().getEmail()!=null)
                    {
                        emailText.setText(baseInfo.getData().getEmail());
                    }

                    if(baseInfo.getData().getDomain()!=null)
                    {
                        dizhiDetailText.setText(baseInfo.getData().getDomain());
                    }
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                Toast.makeText(BaseInfoActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
            }

        }) ;


        MyApplication.getQueue().add(stringRequest);

    }
}
